package SocketHomework;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Client {
    private Socket socket;

    public Client()
    {
        try {
            System.out.println("connecting");
            socket=new Socket("localhost",8088);
            System.out.println("connect finish");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start()
    {
        try {
            Scanner scanner = new Scanner(System.in);
            OutputStream os=socket.getOutputStream();
            while(true)
            {
                String input=scanner.nextLine();
                if("exit".equals(input))
                    break;
                byte[] bytes =input.getBytes(StandardCharsets.UTF_8);
                os.write(bytes.length);
                os.write(bytes);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
